﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WhsRoles.aspx.cs" Inherits="Stock_WhsRoles" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>仓库权限设定</title>
    <link href="../js/jquery-easyui-1.2.6/themes/default/easyui.css" rel="stylesheet"
        type="text/css" />
    <script src="../js/jquery-easyui-1.2.6/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script src="../js/json2.js" type="text/javascript"></script>
    <script src="../js/jquery-easyui-1.2.6/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="../js/jquery-easyui-1.2.6/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
    <link href="../css/wms.css" rel="stylesheet" type="text/css" />
    <link href="../css/icon.css" rel="stylesheet" type="text/css" />
    <script src="../js/json2.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <style type="text/css">
        #whsDiv li
        {
        }
        .text-len
        {
            width: 150px;
        }
        .area
        {
            width: 200px;
            height: 150px;
        }
        .txt-left
        {
            text-align: left;
        }
        .txt-rt
        {
            text-align: right;
        }
    </style>
    <script type="text/javascript">
        var page = { handlePrix: "../handleControl/WarehouseMngr.ashx?func=" };
        var whsRoleSet = {
            whsIco: '<img src="../images/icon/home.png" />',
            intail: function (selId) {
                this.loadAllRoles(selId);
                this.loadAllWhs();
            },
            loadAllWhs: function () {
                //GetWhsLst
                jQuery.getJSON(page.handlePrix + "GetWhsLst", { rd: parseInt(Math.random(1) * 10000) }, function (data) {
                    if (data == null || data.list, length < 1)
                        data, list = [];
                    var lis_html = [];
                    for (var i = 0, len = data.list.length; i < len; i++) {
                        var ck = "<input type='checkbox' id='" + data.list[i]["WHS_WHS"] + "'/>";
                        var span = "<span>" + data.list[i]["WHS_WHS"] + "</span>";
                        var li = "<li>" + whsRoleSet.whsIco + ck + span + "</li>";
                        lis_html.push(li);
                    }
                    jQuery("#whsDiv>ul").append(lis_html.join(" "));
                });
            },
            loadAllRoles: function (selId) {
                //roleDrp
                jQuery.getJSON(page.handlePrix + "GetUserRole", { rd: parseInt(Math.random(1) * 10000) }, function (data) {
                    if (data == null || data.list.length < 1)
                        data.list = [];

                    var options = [];
                    for (var i = 0, len = data.list.length; i < len; i++) {
                        var selAtr = " selected='selected'";
                        var opStr = "";
                        if (data.list[i].ID == selId) {
                            opStr = "<option id='" + data.list[i]["ID"] + "' selected='selected' value=" + data.list[i]["ID"] + "'>" + data.list[i]["RoleName"] + "</option>";

                            jQuery("#roleDescArea").html(data.list[i]["RoleDescr"]);
                        }
                        else {
                            opStr = "<option id='" + data.list[i]["ID"] + "' value=" + data.list[i]["ID"] + "'>" + data.list[i]["RoleName"] + "</option>";
                        }
                        options.push(opStr);
                    }
                    jQuery("#roleDrp").empty();
                    jQuery("#roleDrp").append(options.join(" "));

                    whsRoleSet.loadRolesById(selId);
                });

            },
            loadRolesById: function (rid) {
                //GetUserRoleByRoleId
                if (rid == "") {
                    var ckList = jQuery("#whsDiv li :checkbox");
                    ckList.each(function () {
                        var $this = jQuery(this);
                        $this.attr("checked", false);
                    });
                    jQuery("#roleDescArea").val("").val("");
                    return;
                }
                jQuery.getJSON(page.handlePrix + "GetWhsRoleByRoleId", { rd: parseInt(Math.random(1) * 10000), roleId: rid }, function (data) {
                    setVal(data);
                });
                function setVal(data) {
                    if (data.error) {
                        $.messager.alert("提示", data.error, "error");
                        return;
                    }
                    if (data == null || data.list.length < 1) {
                        // $.messager.alert("提示", '没有此权限的数据', "error");
                        return;
                    }

                   // jQuery("#roleDescArea").val("").val(data.list[0].RoleDescr);
                    var whs = data.list[0]["WHS"];
                    var strs = whs.split(",") || "";
                    var ckList = jQuery("#whsDiv li :checkbox");
                    ckList.each(function () {
                        var $this = jQuery(this);
                        $this.attr("checked", false);
                    });
                    if (whs == "" || whs == null) {
                        ckList.each(function () {
                            var $this = jQuery(this);
                            $this.attr("checked", false);
                        });
                    }
                    else {
                        for (var i = 0, len = strs.length; i < len; i++) {
                            ckList.each(function () {
                                var $this = jQuery(this);
                                if ($this.attr("id") == strs[i]) {
                                    $this.attr("checked", true);
                                }
                            });
                        }
                    }
                }
            },
            setWhsRoles: function () {
                var ckList = jQuery("#whsDiv li :checkbox");
                var whsStr = "";
                ckList.each(function () {
                    $this = jQuery(this);
                    if ($this.is(':checked'))
                        whsStr += $this.attr("id") + ",";
                });
                var rId = jQuery("#roleDrp").val();
                if (rId == "") {
                    $.messager.alert("提示", "请选择权限", "error");
                    return;
                }
                var postData = {};
                postData.rName = jQuery("#roleDrp ").find("option:selected").text();
                postData.rid = jQuery("#roleDrp ").find("option:selected").val();
                postData.whs = whsStr;
                postData.rd = parseInt(Math.random(1) * 10000);
                jQuery.post(page.handlePrix + "SetWhsPermiss", postData, function (data) {
                    var msg = JSON.parse(data);
                    if (msg.success) {
                        $.messager.alert("提示", msg.success, "error");
                    }
                    else {
                        $.messager.alert("提示", msg.error, "error");
                    }
                });
            }
        }
        function getQueryString(name,url) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r =url.search.substr(1).match(reg);
            if (r != null) {
           
                return unescape(r[2]);
            }
            return null;
        }


        jQuery(function () {

            var url = jQuery("#form1").attr("action");
            var roleId = url.substring(url.indexOf("id=")+3,url.indexOf("&"))

            whsRoleSet.intail(roleId);
            //whsRoleSet.loadRolesById(selVal);
            //roels dropdown
            //            jQuery("#roleDrp").bind("change", function () {
            //                var selVal = jQuery("#roleDrp").val();
            //                // if (selVal == "") { $.messager.alert("提示", "未选择权限", "error"); return; }
            //                whsRoleSet.loadRolesById(selVal);
            //            });
            //save
            jQuery("#btnSave").bind("click", function () {
                whsRoleSet.setWhsRoles();
            });
            //check box
            var ckList = jQuery("#whsDiv li :checkbox");
            ckList.bind("click", function () {
                var $this = jQuery(this);
            });
        });
    </script>
    <div id="main">
        <div>
            <table>
                <tr>
                    <td class="txt-rt">
                        角色：
                    </td>
                    <td class="txt-left">
                        <select id="roleDrp" name="roleDrp" class="text-len" disabled="disabled" >
                        </select>
                    </td>
                </tr>
                <tr>
                    <td class="txt-rt">
                        角色描述：
                    </td>
                    <td class="txt-left">
                        <textarea id="roleDescArea" name="roleDescArea" class="area" readonly="readonly"></textarea>
                    </td>
                </tr>
                <tr>
                    <td class="txt-rt">
                        仓库：
                    </td>
                    <td class="txt-left">
                        <div id="whsDiv" style="overflow-y: auto;">
                            <ul>
                            </ul>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align: center;">
                        <input type="button" id="btnSave" name="btnSave" value="保存" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    </form>
</body>
</html>
